package site.jlopen.secret.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.lang.TypeReference;
import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.model.node.SecretNodeCreateReq;
import site.jlopen.secret.model.node.SecretNodeInfoResp;
import site.jlopen.secret.service.intf.SecretNodeManage;

public class SecretNodeImpl implements SecretNodeManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretNodeImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<SecretNodeInfoResp> createNode(SecretNodeCreateReq form) {
		logger.info("创建节点请求：{}", form);
		SecretClientHttp<SecretNodeInfoResp> clientRequest = new SecretClientHttp<SecretNodeInfoResp>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<SecretNodeInfoResp> res = clientRequest.execute(SecretReqUrl.NODE_CREATE_URL, form, new TypeReference<SecretClientResponse<SecretNodeInfoResp>>() {});
		return res;
	}

	@Override
	public SecretClientResponse<SecretNodeInfoResp> updateNode(SecretNodeCreateReq form) {
		logger.info("修改节点请求：{}", form);
		SecretClientHttp<SecretNodeInfoResp> clientRequest = new SecretClientHttp<SecretNodeInfoResp>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<SecretNodeInfoResp>  res = clientRequest.execute(SecretReqUrl.NODE_UPDATE_URL, form, new TypeReference<SecretClientResponse<SecretNodeInfoResp>>() {});
		return res;
	}

	@Override
	public SecretClientResponse<String> removeNode(String nodeCode) {
		logger.info("移除{}节点状态", nodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("nodeCode", nodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.NODE_REMOVE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> reStatusNode(String nodeCode) {
		logger.info("刷新{}节点状态", nodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("nodeCode", nodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.NODE_RESTATUS_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<SecretNodeInfoResp> getNode(String nodeCode) {
		logger.info("查询{}节点信息", nodeCode);
		SecretClientHttp<SecretNodeInfoResp> clientRequest = new SecretClientHttp<SecretNodeInfoResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("nodeCode", nodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretNodeInfoResp>  res = clientRequest.execute(SecretReqUrl.NODE_INFO_URL, form, new TypeReference<SecretClientResponse<SecretNodeInfoResp>>() {});
		return res;
	}

	@Override
	public SecretClientResponse<List<SecretNodeInfoResp>> getNodes() {
		logger.info("获取所有节点信息");
		SecretClientHttp<List<SecretNodeInfoResp>> clientRequest = new SecretClientHttp<List<SecretNodeInfoResp>>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().appId(clientProperties.getAppId());
		SecretClientResponse<List<SecretNodeInfoResp>>  res = clientRequest.execute(SecretReqUrl.NODE_LIST_URL, form, new TypeReference<SecretClientResponse<List<SecretNodeInfoResp>>>() {});
		return res;
	}

}
